package memento_pattern;

/**
 * 表示棋盘的类
 * @author 申书航
 * @version 1.0
 */
public class Chessboard {

    private final int MAX_ROW = 10;

    private final int MAX_COL = 10;

    private char[][] chessmen;

    // 构造函数，初始化棋盘
    public Chessboard() {
        chessmen = new char[MAX_ROW][MAX_COL];
    }

    // 刷新显示棋盘上的棋子
    public void refreshShow() {
        for (int i = 0; i < MAX_ROW; i++) {
            for (int j = 0; j < MAX_COL; j++) {
                char c = chessmen[i][j];
                if (c == '\0') {
                    System.out.print("□");
                }
                else {
                    System.out.print(c);
                }
            }
            System.out.println();
        }
    }

    // 添加棋子并刷新显示
    public void addChessman(Chessman chessman) {
        chessmen[chessman.getX() - 1][chessman.getY() - 1] = chessman.getColor();
        refreshShow();
    }

    // 保存当前棋盘状态为备忘录
    public ChessboardMemento save() {
        char[][] clone = new char[MAX_ROW][MAX_COL];
        for (int i = 0; i < MAX_ROW; i++) {
            System.arraycopy(chessmen[i], 0, clone[i], 0, MAX_COL);
        }
        return new ChessboardMemento(clone);
    }

    // 从备忘录恢复棋盘状态
    public void restore(ChessboardMemento memento) {
        this.chessmen = memento.getChessmen();
        refreshShow();
    }
}
